package com.zoulj.config.handler;

import com.zoulj.error.BusinessException;
import com.zoulj.pojo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;

/**
 * Rest控制器异常处理类
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public R globalException(HttpServletResponse response, BusinessException ex){

        log.info("错误代码："  + response.getStatus());
        log.error("GlobalExceptionHandler...",ex);
        for(StackTraceElement ste : ex.getStackTrace()){
            log.error("GlobalExceptionHandler...{}",ste.toString());
        }
        R result = new R(ex.getErrCode()
                ,ex.getErrMsg());
        return result;
    }

}
